const express = require("express");
const { singers } = require("./singer.json");
const app = express();

app.get("/:id.html", (req, res) => {
  const { id } = req.params; // 获取路由参数
  const result = singers.find((item) => {
    return item.id == id;
  }); // 查找匹配的歌手
  if (!result) {
    res.status(404); // 状态码
    res.send("<h1>404</h1>");
    return;
  }
  res.send(`
  <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
    </head>
    <body>
        <h2>${result.singer_name}</h2>
        <img src="${result.singer_pic}"/>
    </body>
    </html>
  `);
});
// 监听端口
app.listen(8000, () => {
  console.log("服务已经启动...");
});
